/*
 * Liscense: 	GPL http://www.gnu.org/copyleft/gpl.html
 * By: 		A. Tres Finocchiaro
 * Date:	2008
 * Java:	6.0SE
 */

package com.pentajock.docdb.utils;

import com.pentajock.docdb.database.*;
import java.util.*;
import java.io.*;

/**
 *
 * @author A. Tres Finocchiaro
 */

/*
 * Filter for listing only supported document types, such as .doc, .xls, .pdf,
 * etc.
 */
public class DocumentFileFilter implements FilenameFilter {
        List<String> extensions;
        public boolean accept(File dir, String name) {
            if (extensions == null)
                extensions = DBController.getExtensions();

            return validType(name) && isTempFile(name);
        }
        
        /*
         * Tries to determine if the file is a temporary file
         */
        public boolean isTempFile(String name) {
            if (name == null)
                return true;
            return name.startsWith("~");
        }
        
        /*
         * Determines if the file is a supported type
         */
        public boolean validType(String name) {
            if (name == null)
                return false;
            return extensions.contains(Utils.getExtension(name).toLowerCase());
        }
        
        /*
         * Refreshes the extensions listing in the event they were changed
         * while using the application
         */
        public void refresh() {
                extensions = null;
        }
}
